<?php
class user_api extends common_api {	 

	
	
	
	
	//apk使用记录,  ios  set_ios_log
    public function set_apk_log($data){
        $phone_imei = trim($data['imei']);
        $channel = trim($data['channel']);
        $type = trim($data['type']);
		$brand = trim($data['brand']);
		$model = trim($data['model']);
		$sdk = trim($data['sdk']);
        //记录IMEI码统计信息
        load_file('imei_class',LIB_PATH);
        $Imei = new Imei($this->db);
        switch($type){
            case 'open':    //打开软件
                $res = $Imei->open_soft($phone_imei,$channel,$brand,$model,$sdk);
                break;
            case 'close':   //关闭软件
                $res = $Imei->close_soft($phone_imei);
                break;          
            default:
                $output = array('status'=>'0','tips'=>'参数错误!');
                break;
        }
        $output['status'] = $res?1:0;
        return $output;
    }   
	 
	 //用户登录
    public function user_login($data) {
		$user_name = trim($data['name']);
		$user_pwd = trim($data['pwd']);
		$imei = trim($data['imei']);
	
		if (!$imei) {
			$output['status'] = '0';
			$output['title'] = '登录失败';
			$output['tips'] = '请打开IMEI码的访问权限';
			return $output;
		} elseif (!preg_match("/^[a-zA-Z0-9]{14,17}$/i", $imei)) {   //imei码不正确
			$output['status'] = '0';
			$output['title'] = '登录失败';
			$output['tips'] = '本功能暂只能通过手机登录访问';
			return $output;
		}
		
		//判断用户和密码是否为空
		if(empty($user_name) ||  empty($user_pwd)){
			$output['status'] = '0';
			$output['title'] = '登录失败';
			$output['tips'] = '请准确输入用户名与密码';
			return $output;
		}
		
		$where = "user_name='".inputSQL($user_name)."' AND user_status='1' AND user_pwd=MD5(CONCAT(MD5('".$user_pwd."'),user_salt))";

	
		//查询用户信息
		$sql = "SELECT user_id,user_name,user_salt,user_status,user_close_reason,user_token FROM data_user where ".$where;
		$row = $this->db->getRow($sql);		
		if (!$row){
			$output['status'] = '0';
			$output['title'] = '登录失败';
			$output['tips'] = '用户名或者密码输入错误';
			return $output;	
		}
		
		if ($row['user_status'] == '1') {    //允许登录
			

			$output = $row;
			$token = $this->get_user_token($row['user_id'],$row['user_salt']);
			unset($output['user_salt']);
			if($token){
				session_start();
				$_SESSION['user_auth'] = $output;
				$this->db->update("data_user",array("user_last_login"=>date("Y-m-d H:i:s"),"user_login_count"=>"user_login_count+1","user_token"=>$token),"user_id='".$output['user_id']."'");
				
				$output['sessionid'] = session_id();
				$output['user_token'] = $token;
				$output['status'] = '1';
			}else{
				return array('status'=>'0','tips'=>'token生成失败');	
			}
			
			return $output;
		} else {	//限制登录
			$output['status'] = '0';
			$output['title'] = '登录失败';
			$output['tips'] = '您的账号被限制登录(' . $row['user_close_reason'] . ')';
			return $output;
		}
	
    }   
  

	//用户账户信息
    public function get_user_info($data) {
        $uid = intval($data['uid']);		
        $fields = " user_name,user_phone,user_sex,user_age,user_education,user_industry,user_position,user_income,user_score,user_allow_edit";
       	$output = $this->db->getRow("select".$fields." from data_user where user_id='" . $uid . "'");
        $output = $this->clean_null($output);
        if ($output) {         
            //$output['user_photo'] = $output['user_photo']?trim(IMG_HOST,'/').get_thumb_name($output['user_photo'],'small'):'';
            $output['status'] = '1';
        } else {
            $output['status'] = '0';
            $output['title'] = '错误提示';
            $output['tips'] = '无该用户信息';
        }
        return $output;
    }
	
		
	private function get_user_token($id,$salt){
		$time = time()+3600*24*30;
		$data = $id."|".$salt."|".$time;
		load_file('crypt_class', LIB_PATH);
		$crypt = new Crypt(API_KEY);
		$token = $crypt->encrypt($data);
		$update = array(
			'user_token'=>$token,
			'user_last_login'=>date("Y-m-d H:i:s"),
			'user_login_count'=>"user_login_count+1"
		);
		return $token;
		
	}
	
	
	public function check_user_token($data){
		$token = $data['token'];
		$uid = intval($data['uid']);
		if($token){
			load_file('crypt_class', LIB_PATH);
			$crypt = new Crypt(API_KEY);
			$data = $crypt->decrypt($token);
			$array = explode("|",$data);
			//0 id, 1 salt,2 时间戳
			if($array[0] && $array[1] && $array[2]){

				$row = $this->db->getRow("select user_id,user_name,user_salt,user_status,user_token from data_user where user_id='".$uid."' and user_status='1'");
				if(!$row){
					return array('status'=>'0','tips'=>'没有此用户信息或被限制登录');	
				}
				if(!$row['user_token'] || $token!=$row['user_token'] ){
					return array('status'=>'0','tips'=>'token信息错误');	
				}
				
				if($array[2]<time()){						//过时
					return array('status'=>'0','tips'=>'认证信息已过期');
				}
				
				if($row['user_id']==$array[0] && $row['user_salt']==$array[1]){
					$token = $this->get_user_token($row['user_id'],$row['user_salt']);
					$output = $row;
					unset($output['user_salt']);
					$this->db->update("data_user",array("user_last_login"=>date("Y-m-d H:i:s"),"user_login_count"=>"user_login_count+1","user_token"=>$token),"user_id='".$output['user_id']."'");
					session_start();
					$_SESSION['user_auth'] = $output;
					$output['sessionid'] = session_id();
					$output['user_token'] = $token;
					$output['status'] = '1';

					return $output;
				}else{
					return array('status'=>'0','tips'=>'用户信息认证失败');
				}
			}else{
				return array('status'=>'0','tips'=>'此信息无效');
			}	
		}else{
			return array('status'=>'0','tips'=>'认证失败');	
			
		}

	}
	
	
}

?>
